| No. | Answer | Remark | |
|---|---|---|---|
| 1 | e | Compiler Error | The constructor of R has an explicit call to the super class constructor that includes an instance variable as a parameter. A compiler error is generated since the instance of class R has not yet been fully initialized at the point in time when the super class constructor is invoked. The instance variable R.j still contains the default value of zero rather than the assigned value of 2. |
| 2 | e | Compiler Error | The instance initializer of class R calls P.m1. Since P.m1 may throw an exception, that exception or a super class of the exception must be declared in the throws clause of every constructor of class R. Note: Unlike a named class, an instance initializer of an anonymous class may throw any exception. Prior to Java 1.4, the code example used in this question would not compile. The bug was fixed in version 1.4. The bug ID is 4409174. |
| 3 | c | Prints: 682134 | Class D is loaded and the static initializer is processed before the main method begins to run. No instance of class D is created, therefore, the instance initializer and constructor do not run. The main method invokes method "m" of class C. As a result, C is loaded and the static initializer runs. A new instance of C is being created, so the constructor for class C is invoked. However, the instance initializer runs to completion before the body of the constructor runs. |
| 4 | a | Prints: 5814 | Class B is loaded and the static initializer is processed before the main method begins to run. The use of the A.m() method causes Class A to be loaded and the static initializer to be processed before method A.m() is processed. No instance of either class is created, so no constructor or instance initializer runs. |
| 5 | e | Compiler Error | The constructor for class E contains an explicit call to the super class constructor. Since the super class constructor call is not the first statement of the constructor a compiler error is generated. |
| 6 | e | Compiler Error | The no parameter constructor for class E contains an explicit call to the single parameter constructor. A compiler error is generated because the explicit constructor call is not the first statement of the constructor. |
| 7 | e | Prints: Static Instance Constructor | Static initializers run before instance initializers. |
| 8 | b | Prints: Static Main Constructor | Static initializers run before main. |
| 9 | a | Prints: Static1 string1 Static2 | Static varibles and initializers are evaluated in the order they are defined. |
| 10 | c | Prints: 257 | The main method of class S begins to run and calls R.m1. Before m1 can run, class R and its super class Q are loaded and the static initializers of the super class and the sub class are processed. If any static variables had been declared, they would also have been initialized. After the classes are loaded and initialized, R.m1 runs. Since no new instance of class Q or R were created, the constructors and the instance initializers were not processed. |
| 11 | e | Compiler Error | The single parameter constructor for class B does not have an explicit call to the super class constructor. Therefore, the compiler tries to add an implicit call to the no parameter constructor of the super class. Since the super class has a declaration for a single parameter constructor, the compiler does not implicitly create a no parameter construct. A compiler error is the result. |
| 12 | f | Prints: Static s2 s1 | Static varibles and initializers are evaluated before instance variables. |
| 13 | b | Prints: 682134134 | Class F is loaded and the static initializer is processed before the main method begins to run. No instance of class F is created, therefore, the instance initializer and constructor do not run. The main method invokes method "m" of class E. As a result, E is loaded and the static initializer runs. A new instance of E is being created, so the constructor for class E is invoked causing the instance initializer to run to completion before the body of the constructor is processed. Method E.m then runs. A second instance of Class E is created causing the constructor for class E and the instance initializer for class E to run again followed by method m. Of course, the constructor of the Object class is always called each time a new instance of any Object is created. |
| 14 | e | Prints: 251346 | The main method of class V begins to run and calls for the creation of a new instance of U. Before the new instance can be created, class U and its super class T are loaded and the static initializers of the super class and the sub class are processed. If any static variables had been declared, they would also have been initialized. After the classes are loaded and initialized, the constructor of class U begins to run. The first statement is an implicit call to the super class constructor. The call to the super class constructor has been added by the compiler, since it does not appear in the source code. Before the body of the super class constructor is processed, the instance initializer will run to completion. If any instance variables had been declared, then they would also be initialized before the body of the super class constructor is processed. Control then returns to the sub class constructor and the sub class instance initializer runs to completion before the body of the constructor is processed. |
| 15 | b | Prints: SA SB IA CA IB CB | Static initialization of the super class occurs first followed by static initialization of the sub class. Instance initialization of the super class occurs before instance initialization of the sub class. |
| 16 | a | Prints: 251346 | The main method of class C begins to run and calls for the creation of a new instance of class B. Before the new instance can be created, class B and its super class A must be loaded and the static initializers of the super class and the sub class are processed. If any static variables had been declared, they would also have been initialized. After the classes are loaded and initialized, the instance creation process begins. The single parameter constructor has as its first statement a call to the single parameter super class constructor. Similarly, the super class constructor has as it first statement an implicit call to the no parameter constructor of the Object class. Once the Object class constructor runs to completion, control returns to the constructor of class A. Before the body of the class A constructor is processed, the instance initializer of class A runs to completion. After the body of the class A constructor runs to completion control returns to the constructor of class B. The instance initializer of class B runs to completion before the body of the construct is processed. |
| 17 | b | Prints: 261358 | The main method of class L begins to run and calls for the creation of a new instance of class K. Before the new instance can be created, class K and its super class J must be loaded and the static initializers of the super class and the sub class are processed. If any static variables had been declared, they would also have been initialized. After the classes are loaded and initialized, the instance creation process begins. The single parameter constructor has as its first implicit statement a call to the no parameter super class constructor. The implicit statement is added by the compiler even if it does not appear in the source code. Another implicit feature of a constructor is the fact that all instance variables are initialized and all instance initializer blocks are processed before the body of the constructor is processed. |
| 18 | d | Compiler Error: variable d might not have been initialized. | The compiler is able to recognize that b is definitely assigned after the assignment expression on line 1. Please see JLS section 16.1.7. The compiler is able to recognize that c is definitely assigned in both the "if" and "else" clause of the if/else expression on line 2. Please see JLS section 16.2.7. The compiler looks at the "if" statements on lines 3, 4 and 5 separately and does not look at the combined result of the three. As a result, the compiler recognizes that d may remain unassigned after each. Therefore, the compiler concludes that d is not definitely assigned before the attempted use on line 6. |
| 19 | b | Prints: 2613587 | The main method of class P begins to run and calls for the creation of a new instance of class N. Before the new instance can be created, class N and its super class M are loaded and the static initializers of the super class and the sub class are processed. If any static variables had been declared, they would also have been initialized. After the classes are loaded and initialized, the instance creation process begins. The no parameter constructor has as its first statement a call to the single parameter constructor. The single parameter constructor has as its first statement an implicit call to the no parameter superclass constructor. Since the call to the super class constructor does not appear in the source code, the compiler adds it implicitly. Before the body of the super class constructor is processed the constructor of the Object class is called and runs to completion. Afterward, the instance initializer of class M runs to completion. If any instance variables had been declared, then they would also be initialized. After the body of the super class constructor runs to completion control returns to the sub class constructor. Before the body of the sub class constructor runs, the instance initializer runs to completion. |
| 20 | c | Prints: 27A13689 | The main method of class I begins to run and calls H.m2. Before the m2 method can run, class H and its super class G are loaded and the static initializers of the super class and the sub class are processed. If any static variables had been declared, they would also have been initialized. After the classes are loaded and initialized, H.m2 runs. Since no new instance of class H or G were created, the constructors and the instance initializers were not yet processed. The next line of I.main creates a new instance of H and calls H.m1. The instance creation process begins with a call to the constructor of H. The first line of the construct is an implicit call to the super class construct. The call to the super class constructor does not appear in the source code, so the compiler added it implicitly. Before the body of the super class constructor runs the instance initializer is processed. If any instance variables had been declared, then they would also have been initialized. The body of the super class constructor is then processed before control returns to the sub class constructor. The instance initializer of the sub class is processed before the body of the sub class. Following the instance creation process, the method H.m1 runs. |